package com.crl.manager.state
{
	import flash.utils.Dictionary;

	/**
	 * 状态机; 
	 * @author crl
	 * 
	 */	
	public class StateMachine
	{
		private var states:Dictionary;
		
		protected var current:IState;
		public function StateMachine()
		{
			states=new Dictionary();
		}
		
		public function get(key:String):IState{
			return states[key];
		}
		
		public function add(state:IState):Boolean{
			states[state.type]=state;
			return true;
		}
		public function remove(key:String):Boolean{
			var state:IState=states[key];
			if(state){
				states[key]=null;
				delete states[key];
				
				return true;
			}
			return false;
		}
		
		public function set state(key:String):void{
			if(current){
				current.exit();
			}
			current=get(key);
			if(current){
				if(current.initialized==false){
					current.initialize();
				}
				current.enter();
			}
		}
	}
}